<?php
namespace App\Controllers;
class Travel extends BaseController
{
    public function index()
    {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $records = $places->findAll();



        $table = new \CodeIgniter\View\Table();

        // $Table = new CodeIgniter View Table () ;

        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));


        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
        }

        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright Xavier'
        ];



        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright Xavier'
        ];

        return $table->generate();

        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions



        return $parser->setData(['records' => $records])
            // and have it render the template with those
            ->render('placeslist');
    }



    public function showme($id)
    {
        $places = new \App\Models\Places();
        $record = $places->find($id);

        if (!$record) {
            throw new \CodeIgniter\Exceptions\PageNotFoundException("No travel destination found with id: $id");
        }

        $table = new \CodeIgniter\View\Table();

        // 设置表头  
        $table->setHeading('Field', 'Value');

        // 添加记录信息  
        $table->addRow('Id', $record['id']);
        $table->addRow('Name', $record['name']);
        $table->addRow('Description', $record['description']);
        //$imageHTML = !empty($record['image']) ?  : 'No image available';  alt="' . $record['name'] . '" style="width:200px; height:auto;
        $table->addRow('Image', '<img src="/image/' . $record['image'] . '" >');
        $table->addRow('Link', anchor($record['link'], 'Visit', ['target' => '_blank']));


        // 设置表格样式  
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        // 数据模板准备  
        $fields = [
            'title' => $record['name'],
            'heading' => $record['name'],
            'footer' => 'Copyright Xavier',
        ];

        // 使用解析器返回结合的视图  
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates/top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates/bottom');
    }
}

